﻿using System.ComponentModel;

namespace Neitzel.Irc.Client
{
    /// <summary>
    /// A Message to display
    /// </summary>
    public class DisplayMessage : INotifyPropertyChanged
    {
        #region Lifetime

        /// <summary>
        /// Initializes a new instance of the DisplayMessage class.
        /// </summary>
        public DisplayMessage()
        {
            Id = 0;
            _sender = string.Empty;
            _body = string.Empty;
            _eventTypes = IrcClientEventTypes.None;
        }

        /// <summary>
        /// Initializes a new instance of the DisplayMessage class.
        /// </summary>
        /// <param name="sender">The sender of the message.</param>
        /// <param name="body">The body of the message.</param>
        public DisplayMessage(string sender, string body)
            : this()
        {
            _sender = sender;
            _body = body;
        }

        /// <summary>
        /// Initializes a new instance of the DisplayMessage class.
        /// </summary>
        /// <param name="sender">The sender of the message.</param>
        /// <param name="body">The body of the message.</param>
        /// <param name="eventTypes">Type of IrcClientEvent that caused this message.</param>
        public DisplayMessage(string sender, string body, IrcClientEventTypes eventTypes)
            : this(sender, body)
        {
            _eventTypes = eventTypes;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the sender of the message.
        /// </summary>
        public string Sender
        {
            get { return _sender; }
            set { _sender = value; OnPropertyChanged(this, new PropertyChangedEventArgs("Sender")); }
        }
        private string _sender;

        /// <summary>
        /// Gets or sets the message body.
        /// </summary>
        public string Body
        {
            get { return _body; }
            set { _body = value; OnPropertyChanged(this, new PropertyChangedEventArgs("Body")); }
        }
        private string _body;

        /// <summary>
        /// Gets or sets the type of event that caused this message.
        /// </summary>
        public IrcClientEventTypes EventTypes
        {
            get { return _eventTypes; }
            set { _eventTypes = value; OnPropertyChanged(this, new PropertyChangedEventArgs("EventTypes")); }
        }
        private IrcClientEventTypes _eventTypes;
 
        /// <summary>
        /// Gets the Id of the message
        /// </summary>
        public int Id { get; internal set; }

        #endregion

        #region INotifyPropertyChanged implementation

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            // prevent racing conditions
            var handler = PropertyChanged;

            // trigger event
            handler?.Invoke(sender, e);
        }

        #endregion
    }
}
